<div id="content">
	<ul class="featured-item">
	</ul>
</div>

<div id="sidebar">
	<div class="choice-form">
		<fieldset>
			<div class='checkboxArea' id='only_recommended'></div>
			<label>От cтилиста</label>
		</fieldset>
	</div>
	<ul class="menu">
		<li id='categories'>
		</li>
		<li id='brands'>
		</li>
		<li id='seasons'>
		</li>
		<li id='added'>
		</li>
	</ul>
	{if $isStylist}
		<a href="#" class="add-link" onclick="openShopItemStylist(0,function(){ showGallery(); }); return false;" title="Добавить"></a>
		<br>
	{/if}
</div>

{literal}
<script id='products_tpl' type="text/x-jsmart-tmpl">
	{foreach $products as $product}
	<li item_id='{$product[0]}'>
		<a href="#" class="visual"><img id={$product[0]}></a>
		<a href="#" class="product_title">{$product[4]}</a>
		<strong>{price_fmt price=$product[5]}</strong>
		{if $showDate}
			<div class='date'>{$product[8]}</div>
		{/if}
	</li>
	{foreachelse}
		<div style='text-align:center; color: #999;'>
			Нет вещей отвечающих выбранным параметрам
		</div>
	{/foreach}
</script>
{/literal}


<script>
	var shopFilter = {};
	
	function showGallery() {
		call('admin.getList',{ 'id':'shop_items', 'params':{ 'clientId':clientId }, 'filter':shopFilter }).done(function(products) {
			$('#content ul').empty().append( $('#products_tpl').html().fetch( { 'products':products, 'showDate':('added' in shopFilter ? 1 : 0) } ) );
			
			var onThumbLoad = function(itemId) {
				return function(thumbnail) {
					$('#'+itemId).attr('src',thumbnail);
				}
			};
			for (var i=0; i<products.length; ++i) {
				call('image.getThumbnail', { 'path': products[i][3], 'width':150, 'height':200 } ).done( onThumbLoad(products[i][0]) );
			}
			
			$('#content ul li a').click(function(e){ 
				e.preventDefault(); 
				openShopItem( $(e.target).closest('li').attr('item_id'), function(){ showGallery(); } ); 
			});
			
			call('session.set',{ 'name':'shop_filter', 'value':shopFilter });
		} );
	}

	$(function() {
		call('session.get',{ 'name':'shop_filter' }).done(function(filter){
			if (filter) {
				shopFilter = filter;
			}

			$('#only_recommended').click(function(e){
				if ($(e.target).hasClass('checked')) {
					shopFilter.recommended = 1;
				} else {
					delete shopFilter.recommended;
				}
				showGallery();
			}).addClass(('recommended' in shopFilter)?'checked':'');


			$('#categories').opener({
				'title': 'КАТЕГОРИЯ',
				'list': groups,
				'onClick': function(v) { shopFilter.groupId=v; showGallery(); },
				'onSecondClick': function(v) { delete shopFilter.groupId; showGallery(); },
				'value': shopFilter.groupId,
				'opened': true
			});

			$('#brands').opener({
				'title': 'БРЕНД',
				'list': brands,
				'onClick': function(v) { shopFilter.brand=v; showGallery(); },
				'onSecondClick': function(v) { delete shopFilter.brand; showGallery(); },
				'value': shopFilter.brand
			});
			
			$('#seasons').opener({
				'title': 'СЕЗОН',
				'list': seasons,
				'onClick': function(v) { shopFilter.season = v;  showGallery(); },
				'onSecondClick': function(v) { delete shopFilter.season; showGallery(); },
				'value': shopFilter.season
			});

			$('#added').opener({
				'title': 'ДОБАВЛЕНЫ',
				'list': [[1,1,'Вчера'],[7,7,'Неделя'],[30,30,'Месяц'],[90,90,'3 месяца'],[180,180,'Полгода'],[360,360,'Год']],
				'onClick': function(v) { shopFilter.added=[v,'<=']; showGallery(); },
				'onSecondClick': function(v) { delete shopFilter.added; showGallery(); },
				'value': ('added' in shopFilter) ? shopFilter.added[0] : 0
			});
		
			showGallery();
		});
	});
</script>